package com.lebang.controller.api.earned;

import com.lebang.common.Require;
import com.lebang.common.ValidCode;
import com.lebang.entity.RequestHeaders;
import com.lebang.feign.SleepEarnedFeign;
import com.lebang.model.RequestUserInfo;
import com.lebang.model.back.Result;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.MemberNewTaskService;
import com.lebang.service.MemberNoticeService;
import com.lebang.service.PointService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 睡觉赚
 *
 * @Author: hwq
 * @Date: 2020/1/8 13:56
 * @Version 1.0
 */
@Api("睡觉赚")
@RestController
@RequestMapping("/app/ear/sleep")
public class SleepEarnedApi {
    @Autowired
    private PointService pointService;
    @Autowired
    private MemberNoticeService memberNoticeService;
    @Autowired
    private SleepEarnedFeign sleepEarnedFeign;
    @Autowired
    private MemberNewTaskService memberNewTaskService;

    @Require
    @ApiOperation("金币列表")
    @PostMapping("/info")
    public JsonData info(HttpServletRequest request) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return sleepEarnedFeign.info(userInfo);
    }

    @Require
    @ApiOperation("开始睡觉")
    @PostMapping("/star")
    public JsonData star(HttpServletRequest request) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return sleepEarnedFeign.star(userInfo);
    }

    @Require
    @ApiOperation("唤醒")
    @PostMapping("/end")
    public JsonData end(HttpServletRequest request, @RequestParam(value = "id", required = false) Integer id) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return sleepEarnedFeign.end(userInfo, id);
    }
    @ValidCode
    @Require
    @ApiOperation("点击领取金币")
    @PostMapping("/rise")
    public JsonData rise(HttpServletRequest request, @RequestParam(value = "id", required = false) Integer id) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        RequestHeaders headers = new RequestHeaders(request);
        Result<Integer> result = sleepEarnedFeign.rise(userInfo, id);
        if (result.isSuccess()) {
            pointService.addPoint(userInfo.getMemberId(), result.getData(),
                    TaskCategory.SLEEP_EARNS, headers, "");
            memberNoticeService.addNoticeAndPush("睡觉赚", "完成睡觉赚阶段奖励" + result.getData() + "金币", headers.getUserid(), headers);
           memberNewTaskService.addRecord(6002,userInfo.getMemberId());
            return Callback.success(result.getStatus(), result.getMessage(), result.getData());
        } else {
            return Callback.fail(result.getStatus(), result.getMessage());
        }
    }

}
